﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using Aliyeye.Car.Net.UI.SystemHelp;

namespace Aliyeye.Car.Net.UI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.DoEvents(); 

            // 注册全局线程异常处理事件侦听 
            //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            if (!Login.IsTestUser)
                StartAutoUpdater();

            Application.Run(new Aliyeye.Car.Net.UI.SystemHelp.Login());
            //Application.Run(MainWin.GetInstance());
            //Application.Run(Aliyeye.Car.Net.UI.SystemHelp.Login.GetInstance());
        }

        /// <summary> 
        /// 全局线程异常处理事件 
        /// </summary> 
        /// <param name="sender"> </param> 
        /// <param name="e"> </param> 
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            // 显示全局异常提示 
            //"程序碰到严重错误，请重新操作。或者重启程序，问题还没解决请联系管理员。"

            Common.PageCommon.Alert("程序碰到严重错误，请重新操作。或者重启程序，问题还没解决请联系管理员。");
        }

        static void StartAutoUpdater()
        {
            Aliyeye.Car.Net.AutoUpdater.AutoUpdater au = new Aliyeye.Car.Net.AutoUpdater.AutoUpdater();
            try
            {
                au.Update();
            }
            catch (WebException exp)
            {
                MessageBox.Show(String.Format("无法找到指定资源\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (XmlException exp)
            {
                MessageBox.Show(String.Format("下载的升级文件有错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (NotSupportedException exp)
            {
                MessageBox.Show(String.Format("升级地址配置错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ArgumentException exp)
            {
                MessageBox.Show(String.Format("下载的升级文件有错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception exp)
            {
                MessageBox.Show(String.Format("升级过程中发生错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
